
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Windsea
 */
public class ColorSelectorUI extends Form implements ItemStateListener {

    App midlet;
    private Gauge red = new Gauge("R", true, 255, 0);//red component
    private Gauge green = new Gauge("G", true, 255, 0);//blue component
    private Gauge blue = new Gauge("B", true, 255, 255);//green component
    private StringItem btnSetColor;
    private ShowColor sc = null;
    int color = 0x000000;
    Command cmdsetColor;

    public ColorSelectorUI(final App midlet) {
        super("color picker");
        this.midlet = midlet;
        sc = new ShowColor("Color preview");
        btnSetColor = new StringItem("", "Set Color", StringItem.BUTTON);
        this.append(this.red);
        this.append(this.green);
        this.append(this.blue);
        this.append(sc);
        this.append(btnSetColor);
        cmdsetColor = new Command("Set Color", Command.ITEM, 0);

        btnSetColor.setDefaultCommand(cmdsetColor);
        ItemCommandListener icl = new ItemCommandListener() {
            public void commandAction(Command c, Item item) {
                if (c == cmdsetColor && item == btnSetColor) {
                    midlet.getUIMain().setBGColor(color);
                    midlet.showMenuForm();
                }
            }
        };
        btnSetColor.setItemCommandListener(icl);

        //Register listener
        this.setItemStateListener(this);
    }

    /**
     * Color Preview window
     *
     * @Author flyeverzhang 2009-7-31
     */
    class ShowColor extends CustomItem {

        protected ShowColor(String label) {
            super(label);
        }

        protected int getMinContentHeight() {
            return 30;
        }

        protected int getMinContentWidth() {
            return 150;
        }

        protected int getPrefContentHeight(int arg0) {
            return this.getMinContentHeight();
        }

        protected int getPrefContentWidth(int arg0) {
            return this.getMinContentWidth();
        }

        protected void paint(Graphics g, int w, int h) {
            g.setColor(red.getValue(), green.getValue(), blue.getValue());
            color = g.getColor();
            g.fillRect(0, 0, w - 1, h - 1);
        }

        public void reDraw() {
            this.repaint();
        }
    }

    public void itemStateChanged(Item item) {
        sc.reDraw();
    }
}
